/**
 * Copyright (c) 2020 wuchenxi
 * walter-blog is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * <p>
 * <p>
 * Mulan Permissive Software License，Version 2
 * <p>
 * Mulan Permissive Software License，Version 2 (Mulan PSL v2)
 * January 2020 http://license.coscl.org.cn/MulanPSL2
 */
package com.walter.blog.business.content.controller;


import com.walter.blog.aop.annotation.NeedLogin;
import com.walter.blog.business.content.service.CategoryService;
import com.walter.db.pojo.Category;
import com.walter.response.ResponseKit;
import com.walter.vo.category.CategoryListVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author wuchenxi
 * @since 2020-12-25
 */
@Api(tags = "分类管理")
@Slf4j
@Validated
@RestController
@RequestMapping("/category")
@PreAuthorize("isAuthenticated()")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("/add")
    @ApiOperation(value = "增加分类")
    @ApiResponses(value = {
            @ApiResponse(code = 500, message = "增加失败"),
            @ApiResponse(code = 402, message = "已有该分类，不能重复添加"),
            @ApiResponse(code = 200, message = "增加成功")})
    public ResponseKit<Object> add(@Valid Category category) {

        return categoryService.add(category);
    }

    @GetMapping("/list")
    @ApiOperation(value = "分类列表")
    public ResponseKit<List<CategoryListVO>> list() {
        return categoryService.listCategory();
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除分类")
    public ResponseKit<Object> delete(@ApiParam(name = "分类id", required = true) String categoryId) {
        if (StringUtils.isEmpty(categoryId)) {
            log.warn("categoryId can't be null!");
            return ResponseKit.error(400, "参数错误！请重试！");
        }
        return categoryService.delete(categoryId);
    }


}

